local gancheng = fk.CreateSkill{
  name = "rfenghou_3d18j__gancheng",
}

Fk:addTargetTip{
  name = gancheng.name,
  target_tip = function(self, player, to_select, selected, _, _, selectable, extra_data)
    if not table.contains(extra_data.targets, to_select.id) then return end
    local fusu = extra_data.extra_data.rfenghou_3d18j__ganchengFrom
    if not fusu then return end
    local tip = { {content = "@@CancelTarget", type = "warning"} }
    if not fusu:inMyAttackRange(to_select) then
      table.insert(tip, {content = "draw1", type = "normal"} )
    end
    return tip
  end
}

gancheng:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(gancheng.name) and data.card and data.firstTarget and #data:getAllTargets() > 1 and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = table.filter(player:getCardIds("he"), function(id)
      return Fk:getCardById(id).type == Card.TypeBasic
    end)
    if #cards == 0 then return end
    if room:askToSkillInvoke(player, {
      skill_name = gancheng.name,
      prompt = "#rfenghou_3d18j__gancheng-invoke:::" .. data.card:toLogString()
    }) then
      local tos = table.filter(room:getAlivePlayers(), function(to)
        return to == player or player:inMyAttackRange(to)
      end)
      event:setCostData(self, { cards = cards, tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local cards = dat.cards
    room:throwCard(cards, gancheng.name, player, player)
    local tos = table.filter(dat.tos, function(to)
      return to:isAlive()
    end)
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      local can_cancel = data:getAllTargets()
      if #can_cancel == 0 then break end
      local cancel = room:askToChoosePlayers(to, {
        targets = can_cancel,
        min_num = 1,
        max_num = 1,
        skill_name = gancheng.name,
        prompt = "#rfenghou_3d18j__gancheng-cancel:" .. player.id .. "::" .. data.card:toLogString(),
        target_tip_name = gancheng.name,
        extra_data = { rfenghou_3d18j__ganchengFrom = player },
        cancelable = false,
      })[1]
      if cancel then
        data:cancelTarget(cancel)
        if not player:inMyAttackRange(cancel) then
          room:drawCards(player, 1, gancheng.name)
        end
      end
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__gancheng"] = "干城",
  [":rfenghou_3d18j__gancheng"] = "一张牌指定多名角色为目标时，你可以弃置所有基本牌，令你和攻击范围内的角色依次取消一名目标；每有一名你攻击范围外的角色被选择，你摸一张牌。",

  ["#rfenghou_3d18j__gancheng-invoke"] = "干城：你可以弃置所有基本牌，令你和攻击范围内的角色依次为 %arg 取消一名目标",
  ["#rfenghou_3d18j__gancheng-cancel"] = "干城：请为 %arg 取消一名目标，若其位于 %src 攻击范围外，%src 摸一张牌",
}

return gancheng
